﻿/*
* sys_roleinfo.cs
* 2017-05-17
* 功 能： 角色数据操作类
* 类 名： sys_roleinfo
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using FZKJ.DBUtility;
using System.Collections.Generic;//Please add references
namespace FZKJ.DAL
{
    /// <summary>
    /// 数据访问类:sys_roleinfo
    /// </summary>
    public partial class sys_roleinfo
    {
        public sys_roleinfo()
        { }

        /// <summary>
        /// 获取编号
        /// </summary>
        /// <returns></returns>
        public string GetMaxNo()
        {
            string sql = "SELECT RIGHT(1001+ISNULL(RTRIM(RIGHT(MAX(rolecode),3)),0),4) from sys_roleinfo";
            return new SQLHelper().GetSingle(sql).ToString();
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(FZKJ.Model.sys_roleinfo MODEL)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into sys_roleinfo(");
            strSql.Append("rolecode,rolename,roletype,isenable,rolesort,remark,createtime,createman,updatetime,updateman)");
            strSql.Append(" values (");
            strSql.Append("@rolecode,@rolename,@roletype,@isenable,@rolesort,@remark,@createtime,@createman,@updatetime,@updateman)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@rolecode", SqlDbType.NVarChar,10),
                    new SqlParameter("@rolename", SqlDbType.NVarChar,10),
                    new SqlParameter("@roletype", SqlDbType.NVarChar,10),
                    new SqlParameter("@isenable", SqlDbType.Int,4),
                    new SqlParameter("@rolesort", SqlDbType.Int,4),
                    new SqlParameter("@remark", SqlDbType.NVarChar,200),
                    new SqlParameter("@createtime", SqlDbType.DateTime),
                    new SqlParameter("@createman", SqlDbType.NVarChar,36),
                    new SqlParameter("@updatetime", SqlDbType.DateTime),
                    new SqlParameter("@updateman", SqlDbType.NVarChar,36)};
            parameters[0].Value = MODEL.rolecode;
            parameters[1].Value = MODEL.rolename;
            parameters[2].Value = MODEL.roletype;
            parameters[3].Value = MODEL.isenable;
            parameters[4].Value = MODEL.rolesort;
            parameters[5].Value = MODEL.remark;
            parameters[6].Value = MODEL.createtime;
            parameters[7].Value = MODEL.createman;
            parameters[8].Value = MODEL.updatetime;
            parameters[9].Value = MODEL.updateman;

            object obj = new SQLHelper().GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(FZKJ.Model.sys_roleinfo MODEL)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update sys_roleinfo set ");
            strSql.Append("rolename=@rolename,");
            strSql.Append("roletype=@roletype,");
            strSql.Append("isenable=@isenable,");
            strSql.Append("rolesort=@rolesort,");
            strSql.Append("remark=@remark,");
            strSql.Append("createtime=@createtime,");
            strSql.Append("createman=@createman,");
            strSql.Append("updatetime=@updatetime,");
            strSql.Append("updateman=@updateman");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@rolename", SqlDbType.NVarChar,10),
                    new SqlParameter("@roletype", SqlDbType.NVarChar,10),
                    new SqlParameter("@isenable", SqlDbType.Int,4),
                    new SqlParameter("@rolesort", SqlDbType.Int,4),
                    new SqlParameter("@remark", SqlDbType.NVarChar,200),
                    new SqlParameter("@createtime", SqlDbType.DateTime),
                    new SqlParameter("@createman", SqlDbType.NVarChar,36),
                    new SqlParameter("@updatetime", SqlDbType.DateTime),
                    new SqlParameter("@updateman", SqlDbType.NVarChar,36),
                    new SqlParameter("@id", SqlDbType.Int,4),
                    new SqlParameter("@rolecode", SqlDbType.NVarChar,10)};
            parameters[0].Value = MODEL.rolename;
            parameters[1].Value = MODEL.roletype;
            parameters[2].Value = MODEL.isenable;
            parameters[3].Value = MODEL.rolesort;
            parameters[4].Value = MODEL.remark;
            parameters[5].Value = MODEL.createtime;
            parameters[6].Value = MODEL.createman;
            parameters[7].Value = MODEL.updatetime;
            parameters[8].Value = MODEL.updateman;
            parameters[9].Value = MODEL.id;
            parameters[10].Value = MODEL.rolecode;

            int rows = new SQLHelper().ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sys_roleinfo ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            int rows = new SQLHelper().ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string rolecode)
        {
            List<CommandInfo> cmd_list = new List<CommandInfo>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sys_roleinfo ");
            strSql.Append(" where rolecode=@rolecode ");
            SqlParameter[] parameters = {
                    new SqlParameter("@rolecode", rolecode)};
            cmd_list.Add(new CommandInfo(strSql.ToString(), parameters));
            int rows = new SQLHelper().ExecuteSqlTran(cmd_list);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sys_roleinfo ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = new SQLHelper().ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FZKJ.Model.sys_roleinfo GetModel(string rolecode)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,rolecode,rolename,roletype,isenable,rolesort,remark,createtime,createman,updatetime,updateman from sys_roleinfo ");
            strSql.Append(" where rolecode=@rolecode");
            SqlParameter[] parameters = {
                    new SqlParameter("@rolecode", rolecode)
            };

            FZKJ.Model.sys_roleinfo MODEL = new FZKJ.Model.sys_roleinfo();
            DataSet ds = new SQLHelper().Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FZKJ.Model.sys_roleinfo DataRowToModel(DataRow row)
        {
            FZKJ.Model.sys_roleinfo MODEL = new FZKJ.Model.sys_roleinfo();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    MODEL.id = int.Parse(row["id"].ToString());
                }
                if (row["rolecode"] != null)
                {
                    MODEL.rolecode = row["rolecode"].ToString();
                }                
                if (row["rolename"] != null)
                {
                    MODEL.rolename = row["rolename"].ToString();
                }
                if (row["roletype"] != null)
                {
                    MODEL.roletype = row["roletype"].ToString();
                }
                if (row["isenable"] != null && row["isenable"].ToString() != "")
                {
                    MODEL.isenable = int.Parse(row["isenable"].ToString());
                }
                if (row["rolesort"] != null && row["rolesort"].ToString() != "")
                {
                    MODEL.rolesort = int.Parse(row["rolesort"].ToString());
                }
                if (row["remark"] != null)
                {
                    MODEL.remark = row["remark"].ToString();
                }
                if (row["createtime"] != null && row["createtime"].ToString() != "")
                {
                    MODEL.createtime = DateTime.Parse(row["createtime"].ToString());
                }
                if (row["createman"] != null)
                {
                    MODEL.createman = row["createman"].ToString();
                }
                if (row["updatetime"] != null && row["updatetime"].ToString() != "")
                {
                    MODEL.updatetime = DateTime.Parse(row["updatetime"].ToString());
                }
                if (row["updateman"] != null)
                {
                    MODEL.updateman = row["updateman"].ToString();
                }
            }
            return MODEL;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,rolecode,rolename,roletype,isenable,rolesort,remark,createtime,createman,updatetime,updateman ");
            strSql.Append(" FROM sys_roleinfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return new SQLHelper().Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,rolecode,rolename,roletype,isenable,rolesort,remark,createtime,createman,updatetime,updateman ");
            strSql.Append(" FROM sys_roleinfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return new SQLHelper().Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM sys_roleinfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = new SQLHelper().GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from sys_roleinfo T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return new SQLHelper().Query(strSql.ToString());
        }
    }
}

